|
![](/i/fill.gif) |
Thorsten Froehlich wrote:
> In article <3e6bcf10@news.povray.org> , "Anders K." <and### [at] kaseorg com>
> wrote:
>>This has annoyed me a lot. If they fixed the broken gradient pattern, why
>>can't they fix the equally broken div() and mod() functions?
>
> Because they are not broken. This is the correct and thus expected
> behavior.
>
>>div(7, 3) = 2
>>div(-7, 3) = -3
>>div(7, -3) = -2
>>div(-7, -3) = 3
>
> How can the result possibly be +-3 for any of these? No matter how you
> round, this makes only sense if you apply *different* rounding rules
> depending on the signs. And doing so is nonsensical.
>
>>mod(7, 3) = 1
>>mod(-7, 3) = 2
>>mod(7, -3) = 1
>>mod(-7, -3) = 2
>
> Again, only if you apply random rules you could arrive at this result. I
> could understand you argue for 1,2,2,1 as results for this, but not 1,2,1,2
> because that is definitely wrong.
why? - ah yes, because *you* say so! ;)
- then consider this:
(source: http://www.cs.uu.nl/~daan/papers/divmodnote.html)
: For any two real numbers D (dividend) and d (divisor) with d != 0,
: there exists a pair of numbers q (quotient) and r (remainder) that
: satisfy the following basic conditions of division:
: (1) q [is an Element of] Z (the quotient is an integer)
: (2) D = d*q + r (division rule)
: (3) |r| < |d|
i think we all agree on that. - so, we can define two functions
(or operators, if you prefer "a + b" over "add(a,b)"), 'div' and
'mod', in order to find q and r for any given D and d:
: q = D div d
: r = D mod d
still no problem. - but unfortunately, this is as far as
the definitions go. as you can easily verify, there may
be more than one solution for a pair (q, r) which satisfy
the above conditions (1)-(3)!
- so, if what Anders wrote is wrong, then *at least one*
of the following equations must be false:
7 = 3 * 2 + 1
-7 = 3 * -3 + 2
7 = -3 * -2 + 1
-7 = -3 * 3 + 2
but, in fact, all are correct. - as my source puts it, this
is because..
: The above conditions don't enforce a unique pair of numbers
: q and r: When div and mod are defined as functions, one has
: to choose a particular pair q and r that satisfy these conditions.
: It is this choice that causes the different definitions found in
: literature and programming languages.
- which is also basically the same statement that Will already made.
in other words: you have to add another rule to this system
in order to make those functions "well-defined" (i hope that
is correct for the german "wohldefiniert" :) - meaning that
if you want a real function that "behaves properly" and returns
the same result for the same input every time, you *must* choose
a way to reduce this ambiguity.
this is what both the programmers of povray, and the engineers
at hp did, and why the results are different, but neither are
*wrong*!
HTH,
g.
--
"I never trust a fighting man who doesn't smoke or drink."
-- Adm William Halsey
++ mailto:gim### [at] psico ch ++ http://www.psico.ch/ ++
Post a reply to this message
|
![](/i/fill.gif) |